//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：DrawFields
// *描述：
// *?2021 MYJL . All rights reserved.
//* ======================================================================
#if UNITY_EDITOR
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
using YooAsset;
namespace HDMCode.Hotfix
{
    public class DrawFields : Editor
    {
        private static int pickFiledCount;
        protected void StarDrawFields(HDMAssetsHotfix script)
        {
            ReadEumeAttribute("资源下载方式", script.PlayMode, (result) => { script.PlayMode = (EPlayMode)Enum.Parse(script.PlayMode.GetType(), (string)result); });

            using (new GUILayout.HorizontalScope())
            {
                switch (script.PlayMode)
                {
                    case EPlayMode.EditorSimulateMode:
                        EditorGUILayout.HelpBox("      资源请求方式：【编辑器本地模式】", MessageType.Info);
                        break;
                    case EPlayMode.OfflinePlayMode:
                        EditorGUILayout.HelpBox("      资源请求方式：【编辑器实机模式】", MessageType.Info);
                        break;
                    case EPlayMode.HostPlayMode:
                        EditorGUILayout.HelpBox("      资源请求方式：【正式服线上模式】", MessageType.Info);
                        break;
                    case EPlayMode.WebPlayMode:
                        EditorGUILayout.HelpBox("      资源请求方式：【正式服Web模式】", MessageType.Info);
                        break;
                }
            }
            GUILayout.Space(2);

            using (new GUILayout.HorizontalScope())
            {
                Texture icon = EditorGUIUtility.IconContent("d_PreMatLight0").image;
                GUILayout.Label(icon, GUILayout.Width(20));
                GUILayout.Label("最大下载次数", GUILayout.ExpandWidth(true));
                GUILayout.Space(60);
                script.downloadingMaxNum = EditorGUILayout.IntField(script.downloadingMaxNum, GUILayout.MaxWidth(800), GUILayout.MinWidth(10));
            }

            GUILayout.Space(2);

            using (new GUILayout.HorizontalScope())
            {
                Texture icon = EditorGUIUtility.IconContent("d_PreMatLight0").image;
                GUILayout.Label(icon, GUILayout.Width(20));
                GUILayout.Label("最大重试次数", GUILayout.ExpandWidth(true));
                GUILayout.Space(60);
                script.failedTryAgain = EditorGUILayout.IntField(script.failedTryAgain, GUILayout.MaxWidth(800), GUILayout.MinWidth(10));
            }

            GUILayout.Space(2);

            using (new GUILayout.HorizontalScope())
            {
                Texture icon = EditorGUIUtility.IconContent("d_PreMatLight0").image;
                GUILayout.Label(icon, GUILayout.Width(20));
                GUILayout.Label("下载资源包名", GUILayout.ExpandWidth(true));
                GUILayout.Space(60);
                script.assetspackageName = EditorGUILayout.TextField(script.assetspackageName, GUILayout.MaxWidth(800), GUILayout.MinWidth(10));
            }

            GUILayout.Space(2);

            using (new GUILayout.HorizontalScope())
            {
                Texture icon = EditorGUIUtility.IconContent("d_PreMatLight0").image;
                GUILayout.Label(icon, GUILayout.Width(20));
                GUILayout.Label("下载补丁包名", GUILayout.ExpandWidth(true));
                GUILayout.Space(60);
                script.scriptPackageName = EditorGUILayout.TextField(script.scriptPackageName, GUILayout.MaxWidth(800), GUILayout.MinWidth(10));
            }

            using (new GUILayout.HorizontalScope())
            {
                Texture icon = EditorGUIUtility.IconContent("d_PreMatLight0").image;
                GUILayout.Label(icon, GUILayout.Width(20));
                GUILayout.Label("设置资源地址", GUILayout.ExpandWidth(true));
                GUILayout.Space(60);
                pickFiledCount = GUILayout.Toolbar(pickFiledCount, new string[] { "测试", "安卓", "苹果", "微端" }, GUILayout.MaxWidth(800), GUILayout.MinWidth(10));
            }

            if (pickFiledCount == 0) SetAddress(ref script.DowanerAdderss.TestDefaultHostServer, ref script.DowanerAdderss.TestFallbackHostServer);
            else if (pickFiledCount == 1) SetAddress(ref script.DowanerAdderss.AndroidDefaultHostServer, ref script.DowanerAdderss.AndroidFallbackHostServer);
            else if (pickFiledCount == 2) SetAddress(ref script.DowanerAdderss.IOSDefaultHostServer, ref script.DowanerAdderss.IOSFallbackHostServer);
            else if (pickFiledCount == 3) SetAddress(ref script.DowanerAdderss.WebDefaultHostServer, ref script.DowanerAdderss.WebFallbackHostServer);
            GUILayout.Space(2);
        }

        /// <summary>
        /// 设置下载地址
        /// </summary>
        /// <param name="mainAddress">默认下载地址</param>
        /// <param name="subAddress">备用下载地址</param>
        void SetAddress(ref string mainAddress, ref string subAddress)
        {
            DrawAddress("默认下载地址", ref mainAddress);
            DrawAddress("备用下载地址", ref subAddress);
            void DrawAddress(string tileName, ref string address)
            {
                using (new GUILayout.HorizontalScope())
                {
                    GUILayout.Space(15);
                    Texture icon = EditorGUIUtility.IconContent("SceneSave").image;
                    GUILayout.Label(icon, GUILayout.Width(20));
                    GUILayout.Label(tileName, GUILayout.ExpandWidth(true));
                    GUILayout.Space(45);
                    address = GUILayout.TextField(address, GUILayout.MaxWidth(800), GUILayout.MinWidth(10));
                }
            }
        }

        /// <summary>
        /// 映射枚举
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="lableName">显示名称</param>
        /// <param name="inputEume">枚举值</param>
        /// <param name="action">回调</param>
        /// <param name="iconName">图标名称</param>
        public void ReadEumeAttribute<T>(string lableName, T inputEume, GenericMenu.MenuFunction2 action, string iconName = "d_PreMatLight0") where T : struct, Enum
        {
            GUILayout.BeginHorizontal();
            Texture icon = EditorGUIUtility.IconContent(iconName).image;
            GUILayout.Label(icon, GUILayout.Width(20));
            GUILayout.Label(lableName, GUILayout.ExpandWidth(true));
            GUILayout.Space(60);
            var selected = inputEume.GetType().GetField(inputEume.ToString());

            if (GUILayout.Button(selected.Name, GUILayout.MaxWidth(800), GUILayout.MinWidth(10)))
            {
                GenericMenu menu = new GenericMenu();
                foreach (var item in Enum.GetNames(inputEume.GetType()))
                {
                    var eumeField = inputEume.GetType().GetField(item);
                    if (selected.Name == eumeField.Name)
                        menu.AddItem(new GUIContent(item), true, action, item);
                    else
                        menu.AddItem(new GUIContent(item), false, action, item);
                }
                menu.ShowAsContext();
            }
            GUILayout.EndHorizontal();
        }
    }
}

#endif